package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 存在的问题：
 *  1.代码存在大量的冗余，尤其是SqlSession对象被反复的构建和销毁，开销大
 *  2.accountDao对象也存在大量冗余
 *  如何解决该问题？
 *      可以考虑将反复被使用的对象存入IOC容器中，提供对象资源的复用性
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired//注入bean
    private AccountDao accountDao;

    @Override
    public void save(Account account) {
//        SqlSession session = SqlSessionUtil.getSession();
//        AccountDao accountDao = session.getMapper(AccountDao.class);
        accountDao.save(account);
//        session.close();
    }
    @Override
    public void update(Account account){
//        SqlSession session = SqlSessionUtil.getSession();
//        AccountDao accountDao = session.getMapper(AccountDao.class);
        accountDao.update(account);
//        session.close();
    }
    @Override
    public void delete(Integer id) {
//        SqlSession session = SqlSessionUtil.getSession();
//        AccountDao accountDao = session.getMapper(AccountDao.class);
        accountDao.delete(id);
//        session.close();
    }
    @Override
    public Account findById(Integer id) {
//        SqlSession session = SqlSessionUtil.getSession();
//        AccountDao accountDao = session.getMapper(AccountDao.class);
        Account account = accountDao.findById(id);
//        session.close();
        return account;
    }
    @Override
    public List<Account> findAll() {
//        SqlSession session = SqlSessionUtil.getSession();
//        AccountDao accountDao = session.getMapper(AccountDao.class);
        List<Account> list = accountDao.findAll();
//        session.close();
        return list;
    }
}
